﻿using Client.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFCustomControlExample.Model;

namespace WPFCustomControlExample.ViewModel
{
    public class PaginationExampleViewModel : ViewModelBase
    {
        #region Fields

        private int _currentPageNumber = 1;
        private int _pageDataCount = 20;
        private int _totalDataCount = 256;

        #endregion

        #region Properties

        public int CurrentPageNumber
        {
            get => _currentPageNumber;
            set
            {
                _currentPageNumber = value;
                UpdatePageData();
                RaisePropertyChanged("CurrentPageNumber");
            }
        }

        public int PageDataCount
        {
            get => _pageDataCount;
            set
            {
                _pageDataCount = value;
                UpdatePageData();
                RaisePropertyChanged("PageDataCount");
            }
        }

        public int TotalDataCount
        {
            get => _totalDataCount;
            set
            {
                _totalDataCount = value;
                RaisePropertyChanged("TotalDataCount");
            }
        }

        public ObservableCollection<PaginationTestData> Datas { get; set; }

        #endregion

        #region Constructors

        public PaginationExampleViewModel()
        {
            Datas = new ObservableCollection<PaginationTestData>();
        }

        #endregion

        #region Private Methods

        private void UpdatePageData()
        {
            if (Datas == null)
            {
                return;
            }

            Datas.Clear();

            //根据选择页和每页可显示的数据计算当前应该添加的数据量
            int addDataCount = 0;
            if (CurrentPageNumber * PageDataCount <= TotalDataCount)
            {
                addDataCount = PageDataCount;
            }
            else
            {
                addDataCount = TotalDataCount - (CurrentPageNumber - 1) * PageDataCount;
            }

            //加入数据
            for (int i = 1; i <= addDataCount; i++)
            {
                PaginationTestData addingData = new PaginationTestData();
                addingData.DataNumber = (CurrentPageNumber - 1) * PageDataCount + i;
                addingData.DataName = string.Format("第{0}条数据", addingData.DataNumber);

                Datas.Add(addingData);
            }
        }

        #endregion

        #region Public Methods
        #endregion
    }
}
